package com.example.prison.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.prison.entity.DeviceManagement;
import com.example.prison.mapper.AttendanceDeviceInfoMapper;
import com.example.prison.mapper.DeviceManagementMapper;
import com.example.prison.mapper.SecurityDeviceInfoMapper;
import com.example.prison.service.DeviceManagementService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
* @author jiang
* @description 针对表【device_management】的数据库操作Service实现
* @createDate 2023-06-20 15:14:12
*/
@Service
public class DeviceManagementServiceImpl extends ServiceImpl<DeviceManagementMapper, DeviceManagement>
    implements DeviceManagementService {

    @Autowired
    private DeviceManagementMapper deviceManagementMapper;

    @Autowired
    private AttendanceDeviceInfoMapper attendanceDeviceInfoMapper;

    @Autowired
    private SecurityDeviceInfoMapper securityDeviceInfoMapper;

    @Override
    public PageInfo<DeviceManagement> get(int pageNumber, int pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(deviceManagementMapper.get());
    }

    @Override
    public PageInfo<DeviceManagement> list(int pageNumber, int pageSize, String type, String deviceNumber){
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(deviceManagementMapper.list(type, deviceNumber));
    }

    @Override
    public int add(DeviceManagement device) {
        String deviceNumber = device.getDeviceNumber();
        if (ObjectUtils.isNotEmpty(deviceManagementMapper.getOneByNumber(deviceNumber))) {
            return -1;
        } else {
//            if(device.getType().equals("安防设备")) {
//                AttendanceDeviceInfo attDevice = new AttendanceDeviceInfo();
//                BeanUtils.copyProperties(device, attDevice);
//                attendanceDeviceInfoMapper.add(attDevice);
//            } else if(device.getType().equals("考勤设备")) {
//                SecurityDeviceInfo seDevice = new SecurityDeviceInfo();
//                BeanUtils.copyProperties(device, seDevice);
//                securityDeviceInfoMapper.add(seDevice);
//            }
            deviceManagementMapper.add(device);
            return 1;
        }
    }

    @Override
    public void delete(Integer[] ids) {
//        for (Integer id : ids) {
//            DeviceManagement device = deviceManagementMapper.getById(id);
//            attendanceDeviceInfoMapper.deleteByNumber(attendanceDeviceInfoMapper.getOneByNumber(device.getDeviceNumber()).getDeviceNumber());
//            securityDeviceInfoMapper.deleteByNumber(securityDeviceInfoMapper.getOneByNumber(device.getDeviceNumber()).getDeviceNumber());
//        }
        deviceManagementMapper.deleteDevice(ids);
    }

    @Override
    public int update(DeviceManagement device, Integer id) {
        DeviceManagement deviceDTO = deviceManagementMapper.getById(id);
        deviceManagementMapper.updateDevice(device, id);
        if(deviceManagementMapper.getListOneByNumber(device.getDeviceNumber()).size()>1) {
            deviceManagementMapper.updateDevice(deviceDTO, id);
            return -1;
        }
//        if(ObjectUtils.isNotEmpty(attendanceDeviceInfoMapper.getOneByNumber(deviceDTO.getDeviceNumber()))) {
//            AttendanceDeviceInfo attDeviceDTO = attendanceDeviceInfoMapper.getOneByNumber(deviceDTO.getDeviceNumber());
//            AttendanceDeviceInfo attDevice = new AttendanceDeviceInfo();
//            attDevice.setDeviceId(attDeviceDTO.getDeviceId());
//            BeanUtils.copyProperties(device, attDevice);
//            attendanceDeviceInfoMapper.updateAttendanceDevice(attDevice, attDeviceDTO.getDeviceId());
//        } else if(ObjectUtils.isNotEmpty(securityDeviceInfoMapper.getOneByNumber(deviceDTO.getDeviceNumber()))) {
//            SecurityDeviceInfo seDeviceDTO = securityDeviceInfoMapper.getOneByNumber(deviceDTO.getDeviceNumber());
//            SecurityDeviceInfo seDevice = new SecurityDeviceInfo();
//            seDevice.setDeviceId(seDeviceDTO.getDeviceId());
//            BeanUtils.copyProperties(device, seDevice);
//            securityDeviceInfoMapper.updateSecurityDevice(seDevice, seDeviceDTO.getDeviceId());
//        }
        return 1;
    }

    @Override
    public DeviceManagement getById(Integer id) {
        return deviceManagementMapper.getById(id);
    }

    @Override
    public PageInfo<DeviceManagement> getByType(int pageNumber, int pageSize, String type) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(deviceManagementMapper.getByType(type));
    }

    @Override
    public PageInfo<DeviceManagement> getByNumber(int pageNumber, int pageSize, String deviceNumber) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(deviceManagementMapper.getByNumber(deviceNumber));
    }

    @Override
    public DeviceManagement getOneByNumber(String deviceNumber) {
        return deviceManagementMapper.getOneByNumber(deviceNumber);
    }

}




